<?php
class item_secretmessage extends template_item
{
	var $default_name = "Secret Message";
	var $default_description = "Use the item to discover a message.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['title'] = "Hidden Message";
			$this->item_config['destination'] = 'screen';
			$this->item_config['message'] = "A secret message for you.";
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Where does the message appear?</b>",
															$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][destination]", array(array('screen',"On screen"),array('pm',"By PM")), $this->item_config['destination']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Where does the message appear?</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][title]", $this->item_config['title']),
													)	);
		
		$message = $this->item_config['message'];
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$message = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $message ) ) );
		}
		else
		{
			$message = $this->ipsclass->parser->pre_edit_parse( $message );
		}
		$html .= $this->ipsclass->adskin->add_td_basic("<b>Stored Message</b><br />".$this->ipsclass->han_editor->show_editor( $message, 'item_'.$this->item['item_id'].'_message' ), 'left', 'tablerow1');
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['title'] = $input['title'];
		$save['destination'] = $input['destination'];
		if($this->ipsclass->input['code'] != 'convert')
		{
			$input['message'] = $this->ipsclass->han_editor->process_raw_post( 'item_'.$this->item['item_id'].'_message' );
		}
		$save['message'] = $this->ipsclass->parser->pre_db_parse( $input['message'] );
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Revealed secret message");
		$this->delete_items($inventory_ids);
		
		$this->item_config['message'] = $this->ipsclass->parser->pre_display_parse($this->item_config['message']);
		
		return <<<EOF
<div class='formsubtitle'>{$this->item_config['title']}</div>
<table class='ipbtable' cellspacing='0' cellpadding='0'>
	<tr>
		<td class='row1' align='left'>
			{$this->item_config['message']}
		</td>
	</tr>
</table>
EOF;
	}
	
	function input_done()
	{
		return $this->item_config['destination'] != 'screen';
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		require_once( ROOT_PATH.'sources/lib/func_msg.php' );
 		$msg_lib           =  new func_msg();
		$msg_lib->ipsclass =& $this->ipsclass;
		$msg_lib->init();
		
		$msg_lib->to_by_id    = $this->ipsclass->member['id'];
		$msg_lib->from_member = $this->ipsclass->member;
		$msg_lib->msg_title   = $this->item_config['title'];
		$msg_lib->msg_post    = $this->item_config['message'];
		$msg_lib->force_pm    = 1;
		
		$msg_lib->send_pm();
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->market_class->redirect($this->ipsclass->base_url."act=Msg&CODE=01",array('url'=>1,'instant'=>1));
	}
}
?>